package com.hustzp.com.xichuangzhu.poetry;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ListView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.poetry.adapter.NotificationListAdapter;
import com.hustzp.com.xichuangzhu.poetry.model.UserNotification;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/31 11:37
 * @Description:一日一赏通知列表
 */
public class NotificationListAct extends FragmentActivity {
    private ListView lvCommentList;
    private List<UserNotification> comments = new ArrayList<>();
    private NotificationListAdapter commentListAdapter;
    private PtrFrameLayout mPtrFrameLayout;
    private LoadMoreListViewContainer loadMoreListViewContainer;
    private int page = 1;
    private int pageCount = 15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_list);
        lvCommentList = (ListView) findViewById(R.id.lvCommentList);
        mPtrFrameLayout = (PtrFrameLayout) findViewById(R.id.load_more_list_view_ptr_frame);

        mPtrFrameLayout.setLoadingMinTime(1000);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

                // here check list view, not content.
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, lvCommentList, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                loadPostComment();
            }
        });

        commentListAdapter = new NotificationListAdapter(this, comments);
        lvCommentList.setAdapter(commentListAdapter);

        // load more container
        loadMoreListViewContainer = (LoadMoreListViewContainer) findViewById(R.id.load_more_list_view_container);
        loadMoreListViewContainer.useDefaultHeader();
        loadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                //加载更多操作
                loadPostCommentMore();
            }
        });
        loadPostComment();
    }

    private void loadPostComment() {
        page = 1;
        loadPosts(page, pageCount);
    }

    private void loadPostCommentMore() {
        page++;
        loadPosts(page, pageCount);
    }

    public void loadPosts(int pageIndex, int pageCount) {
//        LikePost likePost = (LikePost) getIntent().getParcelableExtra(LikePost.class.getSimpleName());
//        AVObject post = AVObject.createWithoutData("objectId", likePost.getObjectId());
        AVQuery<UserNotification> query = AVQuery.getQuery(UserNotification.class);
        query.setCachePolicy(AVQuery.CachePolicy.NETWORK_ELSE_CACHE);
        query.limit(pageCount);
        query.skip((pageIndex - 1) * pageCount);
//        query.whereEqualTo("post", post);
        query.whereEqualTo("kind", 1);
//        query.include("user");
        query.include("sender");
        query.orderByDescending("createdAt");
        query.findInBackground(new FindCallback<UserNotification>() {
            @Override
            public void done(List<UserNotification> list, AVException e) {
                if (list != null && !list.isEmpty()) {
                    if (page == 1) {
                        mPtrFrameLayout.refreshComplete();
                        comments.clear();
                    }
                    comments.addAll(list);
                    commentListAdapter.notifyDataSetChanged();
                    loadMoreListViewContainer.loadMoreFinish(false, true);
                } else {
                    page--;
                    loadMoreListViewContainer.loadMoreFinish(false, false);
                }
            }
        });
    }

    public void onClose(View v) {
        finish();
    }
}
